let msg = {
	"CAMERA": {
		introTitle: "相机权限使用说明",
		introTips: "用于拍摄照片设置头像，拍照等相关功能，请您点击下方允许获取权限以使用相关功能",
		title: "提示",
		tips: "使用该功能需开启相机权限，请先去开启",
		status: false
	},
	"READ_EXTERNAL_STORAGE": {
		introTitle: "存储权限使用说明",
		introTips: "用于读取相册图片，帮您设置头像，请您点击下方允许获取权限以使用相关功能",
		title: "提示",
		tips: "使用该功能需开启存储权限，请先去开启",
		status: false
	},
	"ACCESS_FINE_LOCATION": {
		introTitle: "地址权限使用说明",
		introTips: "用于定位当前位置，匹配最近的工作室，方便快速抵达，请您点击下方允许获取权限以使用相关功能",
		title: "提示",
		tips: "使用该功能需开启地址权限，请先去开启",
		status: false
	},
	"RECORD_AUDIO": {
		introTitle: "录音权限使用说明",
		introTips: "用于语音提示，请您点击下方允许获取权限以使用相关功能",
		title: "提示",
		tips: "使用该功能需开启录音权限，请先去开启",
		status: false
	},
	"CALL_PHONE":{
		introTitle: "电话权限使用说明",
		introTips: "用于快速联系客服，请您点击下方允许获取权限以使用相关功能",
		title: "提示",
		tips: "使用该功能需开启电话权限，请先去开启",
		status: false
	},
}

export const accredit = (key) => {
	let viewplusBox = null
	let viewplusIsShow = null
	return new Promise( async (resolve, reject) => { 
		// #ifdef APP-PLUS
		if (plus.os.name == 'Android') {
			setTimeout(()=>{
				if(viewplusIsShow !=1){
					viewplusBox = viewplus(key)
					viewplusBox.show()
				}
			},300)
			const result = await requestAndroidPermission(key)	
			viewplusIsShow = result
			if (result === 1) {
			    viewplusBox && viewplusBox.hide()
			} else {
				uni.showModal({
					title: msg[key].title,
					content: msg[key].tips,
					confirmText: "立即设置",
					success:(res)=> {
						if (res.confirm) {
							gotoAppPermissionSetting()
						}
						viewplusBox && viewplusBox.hide()
					}
				});
			}
			console.log(result)
			setTimeout(()=>{
				resolve(result);
			},500)
		} else {
			//ios暂不做处理
			resolve(1)
		}
		// #endif
		// #ifndef APP-PLUS
		resolve(1)
		// #endif
	})


}

function requestAndroidPermission(key) {
	return new Promise((resolve, reject) => {
	    plus.android.requestPermissions(
	        [`android.permission.${key}`],
	         (resultObj)=> {
	            var result = 0;
	            for (var i = 0; i < resultObj.granted.length; i++) {
	                var grantedPermission = resultObj.granted[i];
	                result = 1
	            }
	            for (var i = 0; i < resultObj.deniedPresent.length; i++) {
	                var deniedPresentPermission = resultObj.deniedPresent[i];
	                result = 0
	            }
	            for (var i = 0; i < resultObj.deniedAlways.length; i++) {
	                var deniedAlwaysPermission = resultObj.deniedAlways[i];
	                result = -1
	            }
	            resolve(result);
	        },
	        (error) => {
	            console.log('申请权限错误：' + error.code + " = " + error.message);
	            resolve({
	                code: error.code,
	                message: error.message
	            });
	        }
	    );
	});
}

function viewplus(key) {
	const systemInfo = uni.getSystemInfoSync();
	const statusBarHeight = systemInfo.statusBarHeight;
	const navigationBarHeight = systemInfo.platform === 'android' ? 48 : 44;
	let view = new plus.nativeObj.View('permissionStyle', {
		top: '0px',
		left: '0px',
		width: '100%'
	});
	view.draw([{
		tag: 'rect',
		id: 'bg',
		color: 'rgba(0,0,0,0.4)',
		position: {
			top: '0px',
			left: '0px',
			width: '100%',
			height: '100%'
		}
	}, {
		tag: 'rect',
		id: 'content',
		rectStyles: {
			color: '#fff',
			radius: '6px'
		},
		position: {
			top: (navigationBarHeight + statusBarHeight) + 'px',
			left: '5%',
			width: '90%',
			height: "120px",

		}
	}, {
		tag: 'font',
		id: "title",
		text: msg[key].introTitle,
		textStyles: {
			color: '#a1a1a1',
			size: '16px',
			align: 'left',
			whiteSpace: 'normal',
			verticalAlign: "center",
		},
		position: {
			top: (navigationBarHeight + statusBarHeight + 10) + 'px',
			left: '8%',
			width: '84%',
			height: "25px"
		}
	}, {
		tag: 'font',
		id: "tips",
		text: msg[key].introTips,
		textStyles: {
			color: '#a1a1a1',
			size: '14px',
			align: 'left',
			whiteSpace: 'normal',
			verticalAlign: "center",
		},
		position: {
			top: (navigationBarHeight + statusBarHeight + 40) + 'px',
			left: '8%',
			width: '84%',
			height: "80px"
		}
	}])
	return view
}

function gotoAppPermissionSetting() {
	var Intent:any = plus.android.importClass("android.content.Intent");
	var Settings:any = plus.android.importClass(
		"android.provider.Settings");
	var Uri:any = plus.android.importClass("android.net.Uri");
	var mainActivity:any = plus.android.runtimeMainActivity();
	var intent = new Intent();
	intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
	var uri = Uri.fromParts("package", mainActivity.getPackageName(),
		null);
	intent.setData(uri);
	mainActivity.startActivity(intent);
}


// const status = await uni.$ct.permission.accredit('CAMERA')
// if (status != 1) return
// const status2 = await uni.$ct.permission.accredit('READ_EXTERNAL_STORAGE')
// if (status2 != 1) return